//
//  MusicSingleton.m
//  aMoment
//
//  Created by dllo on 15/10/3.
//  Copyright © 2015年 LuckyHan. All rights reserved.
//

#import "MusicSingleton.h"
#import "DDMenuController.h"

static MusicSingleton *music = nil;

@implementation MusicSingleton

- (void)dealloc
{
    [_player release];
    [super dealloc];
}

+ (MusicSingleton *)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        music = [[MusicSingleton alloc] init];//不能release
    });
    return music;
}

- (void)playMusicWith:(NSString *)urlString
{
    //URL相等
    if ([_currentURL isEqualToString:urlString]) {
        if (!_flag) {
            [_player pause];
            _flag = 1;
        } else {
            [_player play];
            _flag = 0;
        }
    }
    //URL不相等
    else {
        _currentURL = urlString;
        
        //暂停上一首歌
        if (_player) {
            [_player pause];
        }
        
        //播放下一首歌
        self.player = [[AVPlayer alloc] initWithURL:[NSURL URLWithString:urlString]];
        [_player play];
        [_player release];
    }
    
    //记录当前播放速度
    [[NSUserDefaults standardUserDefaults] setBool:[[NSString stringWithFormat:@"%.f", _player.rate] boolValue] forKey:@"rate"];
}

@end
